<template>
  <div class="login-container">
    <!-- <div class="thanks" @click="thanks">致谢团队</div> -->
    <div class="netinfo">© 集点卡（Dost-X） 2018. 保留所有权利。<a href="http://www.miitbeian.gov.cn" target="_blank">津ICP备18006343号-1</a></div>
    <!-- <el-form class="card-box login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
      <p style="text-align:center;margin-top:0;color:#b9dcff;">管理员登录</p>
      <el-form-item prop="username">
        <el-input name="username" type="tel" spellcheck="false" v-model="loginForm.username"
          autoComplete="off" placeholder="请输入手机号" class="input-username" maxlength="11">
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input name="password" :type="pwdType" v-model="loginForm.password" autoComplete="off"
          placeholder="请输入登录密码" class="input-pass"/>
      </el-form-item>
      <el-form-item prop="code">
        <el-input name="code" type="text" spellcheck="false" v-model="loginForm.code" autoComplete="off" placeholder="请输入验证码" class="input-code">
          <template slot="append">
            <img @click="getImgVer" :src="imgCode.url" alt="">
          </template>
        </el-input>
      </el-form-item>

      <el-button type="primary" style="width:100%;margin-bottom:30px;margin-top: 16px;" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
      <div class="forget">
        <span  @click="toForget">忘记密码</span>
        <span>|</span>
        <span @click="toResgister">注册</span>
      </div>
    </el-form> -->
  </div>
</template>

<script>
import { imgVerify } from '@/api/login'
import socialSign from './socialsignin'

export default {
  components: { socialSign },
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('手机号不能为空'))
      } else {
        if (!(/^1\d{10}$/.test(value))) {
          callback(new Error('请输入正确的手机号'))
        } else {
          callback()
        }
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 8) {
        callback(new Error('密码不能小于8位'))
      } else if (!(/^(?![^a-zA-Z]+$)(?!\D+$)/.test(value))) {
        callback(new Error('密码必须包含数字、大小写字母、长度不低于8位'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('验证码不能为空'))
      } else {
        if (value.toUpperCase() !== this.imgCode.code) {
          callback(new Error('验证码错误'))
        } else {
          callback()
        }
        callback()
      }
    }
    return {
      imgCode: [],
      loginForm: {
        username: '',
        password: '',
        code: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur', validator: validateCode }]
      },
      pwdType: 'password',
      eyeType: 'eye',
      loading: false,
      showDialog: false
    }
  },
  created() {
    this.getImgVer()
  },
  methods: {
    getImgVer() {
      imgVerify().then(response => {
        this.imgCode = response.data
      })
    },
    thanks() {
      this.$router.push({ path: '/thanks' })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })
            // this.showDialog = true
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    toResgister() {
      this.$router.push({ path: '/resgister/index' })
      console.log(this.$router.currentRoute.fullPath)
    },
    toForget() {
      this.$router.push({ path: '/resgister/forgetPassword' })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  @import "src/styles/mixin.scss";
  $bg:#1d2b36;
  $dark_gray:#e85858;
  $light_gray:#eee;
  .thanks {
    text-align: center;
    position: absolute;
    bottom: 15vh;
    font-size: 14px;
    color: #2e4a66;
    font-weight: bold;
    left: 50%;
    text-decoration: underline;
    cursor: pointer;
    transform: translateX(-50%);
  }
  .netinfo {
    text-align: center;
    position: absolute;
    bottom: 8vh;
    font-size: 16px;
    color: #2e4a66;
    font-weight: bold;
    left: 50%;
    cursor: pointer;
    transform: translateX(-50%);
  }
  .login-container {
    @include relative;
    height: 100vh;
    min-height: 620px;
    background-color: $bg;
    background-image: url("../../assets/common_images/rebg.png");
    background-size: 100%;
    background-repeat: no-repeat;
    background-position-x: center;
    background-position-y: bottom;
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px #293444 inset !important;
      -webkit-text-fill-color: #fff !important;
    }
    .svg-icon {
      color:#e85858;
    }
    input {
      height: 47px;
      color: #fff;
      text-align: center !important;
      border-radius: 0px;
      background: transparent;
      -webkit-appearance: none;
      padding: 12px 5px 12px 20px;
      border-bottom: 1px solid #78a4cf!important;
    }
    .el-input--medium .el-input__inner {
      height: 47px;
      text-align: left;
      padding: 12px;
      border: none;
      background: #648db6;
    }
    .el-input-group__append, .el-input-group__prepend{
      border: solid 1px #ccc;
    }
    .el-input-group__append{
      border-left:0
    }
    .el-input-group__prepend{
      border-right:0;
    }
    .el-input {
      height: 47px;
      width: 100%;
    }
    ::-webkit-input-placeholder {
      color: #92bbe3;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: #e85858;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 26px;
      }
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .login-form {
      position: absolute;
      left: 0;
      right: -35%;
      bottom: 31% !important;
      width: 350px;
      padding: 35px 30px 15px;
      margin: 150px auto 0px;
      background-color: #648db6;
      border-radius: 5px;
      color: #b9dcff;
    }
    .el-form-item {
      border: none;
      background: #648db6;
      border-radius: 5px;
      color: #454545;
      .el-input-group__append {
        background: #648db6;
        border: 1px solid #648db6;
        img {
          width: 138px;
          margin-top: 9px;
          margin-left: 8px;
          cursor: pointer;
        }
      }
    }
    .el-form-item__error {
      color: #bb0000;
      text-align: center;
      width: 100%;
      padding: 5px 0 0 0;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select:none;
    }
    .thirdparty-button{
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
    .el-button--primary {
      height: 48px;
      font-size: 16px;
      border-radius: 48px;
      background: linear-gradient(left, #4fd7f7, #4b9dfa);
      border: 0;
    }
    .el-button--primary:hover {
      background-color: #666;
    }
    .el-input-group__append{
     padding:0
    }
    div.forget {
      text-align:right;
      cursor: pointer;
      color: #b9dcff;
    }
  }
</style>
